package com.sanshi.project.assets.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sanshi.project.common.core.util.R;
import com.sanshi.project.common.log.annotation.SysLog;
import com.sanshi.project.admin.api.dto.InspectRouteDTO;
import com.sanshi.project.admin.api.entity.InspectRouteEntity;
import com.sanshi.project.admin.api.entity.InspectRoutePointMapEntity;
import com.sanshi.project.assets.service.InspectRouteService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.sanshi.project.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 巡检路线
 *
 * @author nikon
 * @date 2024-01-01 17:17:53
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/inspectRoute" )
@Tag(description = "inspectRoute" , name = "巡检路线管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class InspectRouteController {

    private final  InspectRouteService inspectRouteService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param inspectRoute 巡检路线
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('assets_inspectRoute_view')" )
    public R getInspectRoutePage(@ParameterObject Page page, @ParameterObject InspectRouteDTO inspectRoute) {
        LambdaQueryWrapper<InspectRouteEntity> wrapper = Wrappers.lambdaQuery();
        // 路线名称
		wrapper.like(StrUtil.isNotBlank(inspectRoute.getName()),InspectRouteEntity::getName,inspectRoute.getName());
        // 备注
		wrapper.like(StrUtil.isNotBlank(inspectRoute.getRemark()),InspectRouteEntity::getRemark,inspectRoute.getRemark());
        return R.ok(inspectRouteService.page(page, wrapper));
    }

    /**
     * 通过id查询巡检路线
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('assets_inspectRoute_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(inspectRouteService.getByIdDeep(id));
    }

    /**
     * 新增巡检路线
     * @param inspectRoute 巡检路线
     * @return R
     */
    @Operation(summary = "新增巡检路线" , description = "新增巡检路线" )
    @SysLog("新增巡检路线" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('assets_inspectRoute_add')" )
    public R save(@RequestBody InspectRouteEntity inspectRoute) {
        return R.ok(inspectRouteService.saveDeep(inspectRoute));
    }

    /**
     * 修改巡检路线
     * @param inspectRoute 巡检路线
     * @return R
     */
    @Operation(summary = "修改巡检路线" , description = "修改巡检路线" )
    @SysLog("修改巡检路线" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('assets_inspectRoute_edit')" )
    public R updateById(@RequestBody InspectRouteEntity inspectRoute) {
        return R.ok(inspectRouteService.updateDeep(inspectRoute));
    }

    /**
     * 通过id删除巡检路线
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除巡检路线" , description = "通过id删除巡检路线" )
    @SysLog("通过id删除巡检路线" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('assets_inspectRoute_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(inspectRouteService.removeDeep(ids));
    }

    /**
     * 通过id删除巡检路线子表数据
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除巡检路线子表数据" , description = "通过id删除巡检路线子表数据" )
    @SysLog("通过id删除巡检路线子表数据" )
    @DeleteMapping("/child")
    @PreAuthorize("@pms.hasPermission('assets_inspectRoute_del')" )
    public R removeChild(@RequestBody Long[] ids) {
        return R.ok(inspectRouteService.removeChild(ids));
    }

    /**
     * 导出excel 表格
     * @param inspectRoute 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('assets_inspectRoute_export')" )
    public List<InspectRouteEntity> export(InspectRouteEntity inspectRoute,Long[] ids) {
        return inspectRouteService.list(Wrappers.lambdaQuery(inspectRoute).in(ArrayUtil.isNotEmpty(ids), InspectRouteEntity::getId, ids));
    }
}